package com.git.bds.mr.doctor.service;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.git.bds.mr.doctor.convert.DoctorRecordConvert;
import com.git.bds.mr.doctor.enums.UpdateEnum;
import com.git.bds.mr.doctor.mapper.DoctorMapper;
import com.git.bds.mr.doctor.model.domain.Doctor;
import com.git.bds.mr.doctor.model.dto.RecordModifyDTO;
import com.git.bds.mr.doctor.model.dto.RecordReviseDTO;
import com.git.bds.mr.page.PageParam;
import com.git.bds.mr.page.PageResult;
import com.git.bds.mr.patient.mapper.PatientInfoMapper;
import com.git.bds.mr.patient.model.domain.PatientInfo;
import com.git.bds.mr.records.mapper.AmendmentsMapper;
import com.git.bds.mr.records.mapper.RecordsMapper;
import com.git.bds.mr.records.model.domain.Amendments;
import com.git.bds.mr.records.model.domain.Records;
import com.git.bds.mr.records.model.dto.RecordAddDTO;
import com.git.bds.mr.records.model.dto.RecordDTO;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 医生信息表 服务实现类
 * </p>
 *
 * @author 成大事
 * @since 2022-09-19 16:41:33
 */
@Service
public class DoctorServiceImpl extends MPJBaseServiceImpl<DoctorMapper, Doctor> implements DoctorService {

    @Resource
    private RecordsMapper recordsMapper;

    @Resource
    private PatientInfoMapper patientInfoMapper;

    @Resource
    private AmendmentsMapper amendmentsMapper;

    /**
     * 按页获取记录
     *
     * @param pageParam 页面参数
     * @return {@link PageResult}<{@link RecordDTO}>
     */
    @Override
    public PageResult<RecordDTO> getRecordsByPage(PageParam pageParam) {
        IPage<RecordDTO> page = recordsMapper.selectJoinPage(new Page<>(pageParam.getPageNo(), pageParam.getPageSize()), RecordDTO.class,
                new MPJLambdaWrapper<RecordDTO>()
                        .selectAll(Records.class)
                        .eq(Records::getDoctorId, StpUtil.getLoginIdAsLong()));
        return new PageResult<>(page.getRecords(),page.getSize());
    }

    /**
     * 添加记录
     *
     * @param recordAddDTO 记录添加数据到
     * @return {@link Boolean}
     */
    @Override
    public Boolean addRecord(RecordAddDTO recordAddDTO) {
        long doctorId = StpUtil.getLoginIdAsLong();
        Long hospitalId = this.baseMapper.getHospitalInfo(doctorId);
        Long patientId = patientInfoMapper.selectOne(new LambdaQueryWrapper<PatientInfo>()
                .select(PatientInfo::getId)
                .eq(PatientInfo::getPhone, recordAddDTO.getPhone())).getId();
        Records records = DoctorRecordConvert.INSTANCE.toRecord(hospitalId,doctorId,patientId,recordAddDTO);
        return recordsMapper.insert(records) > 0;
    }

    /**
     * 获取按页修订记录
     *
     * @param pageParam 页面参数
     * @return {@link PageResult}<{@link RecordReviseDTO}>
     */
    @Override
    public PageResult<RecordReviseDTO> getRecordsReviseByPage(PageParam pageParam) {
        IPage<RecordReviseDTO> page = amendmentsMapper.selectJoinPage(new Page<>(pageParam.getPageNo(), pageParam.getPageSize()),
                RecordReviseDTO.class,
                new MPJLambdaWrapper<RecordReviseDTO>()
                        .selectAs(Amendments::getId,"submitId")
                        .selectAs(Amendments::getMrId,"recordId")
                        .selectAs(Amendments::getCreateTime,"submitTime")
                        .leftJoin(Records.class, Records::getDoctorId, Amendments::getDoctorId)
                        .eq(Amendments::getDoctorId, StpUtil.getLoginIdAsLong()));
        return new PageResult<>(page.getRecords(),page.getSize());
    }

    /**
     * 修改病历
     *
     * @param recordDTO 记录数据到
     * @return {@link Boolean}
     */
    @Override
    public Boolean reviseRecord(RecordModifyDTO recordDTO) {
        Records records = DoctorRecordConvert.INSTANCE.toRecord(recordDTO);
        recordsMapper.updateById(records);
        return updateAmendment(recordDTO.getSubmitId(), UpdateEnum.MODIFIED.getValue(),recordDTO.getReply());
    }

    @Override
    public Boolean correctRecord(Long id, String reply) {
        return updateAmendment(id,UpdateEnum.CORRECT.getValue(),reply);
    }

    public Boolean updateAmendment(Long id,int type,String reply){
       return amendmentsMapper.update(null,new UpdateWrapper<Amendments>()
                        .set(Amendments.STATUS,type)
                .set(Amendments.REPLAY,reply)
                .eq(Amendments.ID,id)) > 0;
    }

}
